home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / INAME.C < prev    next >
C/C++ Source or Header  |  1992-03-04  |  6KB  |  199 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* iname.c */
  21. /* Name lookup for Ghostscript interpreter */
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "alloc.h"
  25. #include "errors.h"
  26. #include "name.h"
  27. #include "store.h"
  28.  
  29. /* Definitions and structure for the name table. */
  30. /* The first entry is left unused. */
  31. /* 1-character names are the next nt_1char_size entries. */
  32. #define nt_log2_sub_size 7
  33. #define nt_sub_size (1 << nt_log2_sub_size)
  34. #define nt_hash_size 256        /* must be a power of 2 */
  35. #define nt_1char_size 256        /* must cover a full byte */
  36. typedef name name_sub_table[nt_sub_size];
  37. typedef struct {
  38.     ushort hash[nt_hash_size];
  39.     name *table[1 << (16 - nt_log2_sub_size)];    /* name_sub_table */
  40.     ref count;            /* t_integer */
  41. #define nt_count(nt) (uint)((nt)->count.value.intval)
  42. } name_table;
  43. #define name_index_ptr(nt, index)\
  44.   ((nt)->table[(index) >> nt_log2_sub_size] + ((index) & (nt_sub_size - 1)))
  45.  
  46. /* The one and only name table (for now). */
  47. private name_table *the_nt;
  48.  
  49. /* Forward references */
  50. private int name_alloc_sub(P1(name_table *));
  51.  
  52. /* Make a t_name ref out of a name * */
  53. #define make_name(pref, pnm) make_tv(pref, t_name, pname, pnm)
  54.  
  55. /* Initialize the name table */
  56. void
  57. name_init()
  58. {    uint i;
  59.     the_nt = (name_table *)alloc(1, sizeof(name_table), "name_init");
  60.     memset(the_nt, 0, sizeof(name_table));
  61.     make_int(&the_nt->count, 1);
  62.     for ( i = 0; i <= nt_1char_size; i += nt_sub_size )
  63.        {    the_nt->count.value.intval = i + 1;
  64.         name_alloc_sub(the_nt);
  65.        }
  66. }
  67.  
  68. /* Look up or enter a name in the table. */
  69. /* Return 0 or an error code. */
  70. /* The return may overlap the characters of the string! */
  71. /* See name.h for the meaning of enterflag. */
  72. int
  73. name_ref(const byte *ptr, uint isize, ref *pref, int enterflag)
  74. {    register name *pname;
  75.     const byte *cptr;
  76.     ushort size = (ushort)isize;    /* see name.h */
  77.     if ( size == 1 )
  78.        {    pname = name_index_ptr(the_nt, *ptr + 1);
  79.         if ( pname->string_size != 0 )
  80.            {    make_name(pref, pname);
  81.             return 0;
  82.            }
  83.         if ( enterflag < 0 ) return e_undefined;
  84.         pname->index = *ptr + 1;
  85.         pname->next_index = 0;
  86.        }
  87.     else
  88.        {    ushort *phash =
  89.           the_nt->hash +
  90.             ((ushort)string_hash(ptr, size) & (nt_hash_size - 1));
  91.         uint nidx = *phash;
  92.         while ( nidx != 0 )
  93.            {    pname = name_index_ptr(the_nt, nidx);
  94.             if ( pname->string_size == size &&
  95.                  !memcmp(ptr, pname->string_bytes, size)
  96.                )
  97.                {    make_name(pref, pname);
  98.                 return 0;
  99.                }
  100.             nidx = pname->next_index;
  101.            }
  102.         /* Not in table, allocate a new entry. */
  103.         if ( enterflag < 0 ) return e_undefined;
  104.         if ( !(nt_count(the_nt) & (nt_sub_size - 1)) )
  105.            {    int code = name_alloc_sub(the_nt);
  106.             if ( code < 0 ) return code;
  107.            }
  108.         nidx = nt_count(the_nt);
  109.         pname = name_index_ptr(the_nt, nidx);
  110.         pname->index = nidx;
  111.         pname->next_index = *phash;
  112.         *phash = nidx;
  113.         ref_save(&the_nt->count, "name_ref(count)");
  114.         the_nt->count.value.intval = nidx + 1;
  115.        }
  116.     /* Name was not in the table.  Make a new entry. */
  117.     if ( enterflag )
  118.        {    cptr = (const byte *)alloc(size, 1, "name_ref(string)");
  119.         if ( cptr == 0 ) return e_VMerror;
  120.         memcpy((byte *)cptr, ptr, size);
  121.        }
  122.     else
  123.         cptr = ptr;
  124.     pname->string_size = size;
  125.     pname->string_bytes = cptr;
  126.     pname->pvalue = pv_no_defn;
  127.     make_name(pref, pname);
  128.     return 0;
  129. }
  130.  
  131. /* Get the string for a name. */
  132. void
  133. name_string_ref(const ref *pnref /* t_name */,
  134.   ref *psref /* result, t_string */)
  135. {    name *pname = pnref->value.pname;
  136.     make_tasv(psref, t_string, a_read+a_execute, pname->string_size,
  137.           bytes, (byte *)pname->string_bytes);
  138. }
  139.  
  140. /* Convert a t_string object to a name. */
  141. /* Copy the executable attribute. */
  142. int
  143. name_from_string(const ref *psref, ref *pnref)
  144. {    int exec = r_has_attr(psref, a_executable);
  145.     int code = name_ref(psref->value.bytes, r_size(psref), pnref, 1);
  146.     if ( code < 0 ) return code;
  147.     if ( exec ) r_set_attrs(pnref, a_executable);
  148.     return code;
  149. }
  150.  
  151. /* Enter a name during initialization. */
  152. /* Fatal error if the entry fails. */
  153. void
  154. name_enter(const char *str, ref *pref)
  155. {    if ( name_ref((const byte *)str, strlen(str), pref, 0) )
  156.         lprintf1("name_enter failed - %s", str),
  157.         gs_exit(1);
  158. }
  159.  
  160. /* Get the name with a given index. */
  161. void
  162. name_index_ref(uint index, ref *pnref)
  163. {    make_name(pnref, name_index_ptr(the_nt, index));
  164. }
  165.  
  166. /* Get the current name count. */
  167. ushort
  168. name_next_index()
  169. {    return nt_count(the_nt);
  170. }
  171.  
  172. /* Clean up the name table before a restore. */
  173. /* The count will be reset, and added subtables will be freed. */
  174. /* All we have to do is remove initial entries from the hash chains, */
  175. /* since we know they are linked in decreasing index order. */
  176. /* (There will be some spurious non-zero entries in the subtable table, */
  177. /* but this doesn't matter since they will never be accessed.) */
  178. void
  179. name_restore(uint old_count)
  180. {    register ushort *phash = &the_nt->hash[0];
  181.     register uint i;
  182.     for ( i = 0; i < nt_hash_size; phash++, i++ )
  183.        {    while ( *phash >= old_count )
  184.           *phash = name_index_ptr(the_nt, *phash)->next_index;
  185.        }
  186. }
  187.  
  188. /* ------ Internal procedures ------ */
  189.  
  190. /* Allocate the next sub-table. */
  191. private int
  192. name_alloc_sub(name_table *nt)
  193. {    name *sub = (name *)alloc(1, sizeof(name_sub_table), "name_alloc_sub");
  194.     if ( sub == 0 ) return e_VMerror;
  195.     memset(sub, 0, sizeof(name_sub_table));
  196.     nt->table[nt_count(nt) >> nt_log2_sub_size] = sub;
  197.     return 0;
  198. }
  199.